package com.mashiro.quartz.task;

import cn.hutool.extra.mail.MailUtil;
import com.mashiro.common.utils.DateUtils;
import com.mashiro.quartz.domain.WarnInfo;
import com.mashiro.quartz.mapper.WarnInfoMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * 定时任务调度测试
 *
 * @author mashiro
 */
@Component("mashiroTask")
public class MashiroTask {
    @Resource
    private WarnInfoMapper warnInfoMapper;
    @Value("${mashiro.project.warnDay:3}")
    private int warnDay;

    public void sendWarnMail(Boolean mailFlag) {
        if (mailFlag) {
            Date currentDate = DateUtils.getNowDate();
            List<WarnInfo> warnInfoList = warnInfoMapper.selectUserEmailListByManager(warnDay);
            Map<String, List<WarnInfo>> warnInfoMap = new HashMap<>();
            for (WarnInfo warnInfo : warnInfoList) {
                String email = warnInfo.getEmail(); // 获取 WarnInfo 对象的 email 属性
                if (email != null && !email.isEmpty()) {
                    if (warnInfoMap.containsKey(email)) {
                        // 如果 HashMap 中已经存在对应的 email，将当前 WarnInfo 对象添加到列表中
                        warnInfoMap.get(email).add(warnInfo);
                    } else {
                        // 如果 HashMap 中不存在对应的 email，创建一个新的列表，并将当前 WarnInfo 对象放入列表中
                        List<WarnInfo> newWarnInfoList = new ArrayList<>();
                        newWarnInfoList.add(warnInfo);
                        warnInfoMap.put(email, newWarnInfoList);
                    }
                }
            }
            warnInfoMap.forEach((email, warnInfos) -> {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("<!DOCTYPE html>\n");
                stringBuilder.append("<html lang=\"en\">\n");
                stringBuilder.append("  <head>\n");
                stringBuilder.append("    <meta charset=\"utf-8\">\n");
                stringBuilder.append("    <title>农业科技项目管理系统</title>\n");
                stringBuilder.append("    <style>\n");
                stringBuilder.append("      body {\n");
                stringBuilder.append("        font-family: Arial, Helvetica, sans-serif;\n");
                stringBuilder.append("        background-color: #F7F7F7;\n");
                stringBuilder.append("        padding: 20px;\n");
                stringBuilder.append("      }\n");
                stringBuilder.append("      .container {\n");
                stringBuilder.append("        max-width: 600px;\n");
                stringBuilder.append("        margin: auto;\n");
                stringBuilder.append("        background-color: white;\n");
                stringBuilder.append("        padding: 20px;\n");
                stringBuilder.append("        border-radius: 5px;\n");
                stringBuilder.append("        box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.15);\n");
                stringBuilder.append("      }\n");
                stringBuilder.append("      h1 {\n");
                stringBuilder.append("        color: #0070C0;\n");
                stringBuilder.append("        margin-top: 0;\n");
                stringBuilder.append("      }\n");
                stringBuilder.append("      p {\n");
                stringBuilder.append("        margin-top: 0;\n");
                stringBuilder.append("      }\n");
                stringBuilder.append("<style>\n");
                stringBuilder.append("  table {\n");
                stringBuilder.append("    border-collapse: collapse;\n");
                stringBuilder.append("    width: 100%;\n");
                stringBuilder.append("  }\n");
                stringBuilder.append("  th,\n");
                stringBuilder.append("  td {\n");
                stringBuilder.append("    text-align: left;\n");
                stringBuilder.append("    padding: 8px;\n");
                stringBuilder.append("    border: 1px solid #ddd;\n");
                stringBuilder.append("  }\n");
                stringBuilder.append("  th {\n");
                stringBuilder.append("    background-color: #f2f2f2;\n");
                stringBuilder.append("  }\n");
                stringBuilder.append("</style>");
                stringBuilder.append("    </style>\n");
                stringBuilder.append("  </head>\n");
                stringBuilder.append("  <body>\n");
                stringBuilder.append("    <div class=\"container\">\n");
                stringBuilder.append("      <h1>项目预警</h1>\n");
                stringBuilder.append("<table>\n");
                stringBuilder.append("  <thead>\n");
                stringBuilder.append("    <tr>\n");
                stringBuilder.append("      <th>项目标识</th>\n");
                stringBuilder.append("      <th>项目名</th>\n");
                stringBuilder.append("      <th>天数</th>\n");
                stringBuilder.append("    </tr>\n");
                stringBuilder.append("  </thead>\n");
                stringBuilder.append("  <tbody>\n");
                warnInfos.forEach((warnInfo) -> {
                    stringBuilder.append("    <tr>\n");
                    stringBuilder.append("      <td>" + warnInfo.getProjectId() + "</td>\n");
                    stringBuilder.append("      <td>" + warnInfo.getProjectName() + "</td>\n");
                    if (warnInfo.getDiffTime() > 0) {
                        stringBuilder.append("      <td>距截止时间仅" + warnInfo.getDiffTime() + "天</td>\n");
                    } else {
                        stringBuilder.append("      <td>项目已超时" + Math.abs(warnInfo.getDiffTime()) + "天</td>\n");
                    }
                    stringBuilder.append("    </tr>\n");
                });
                stringBuilder.append("  </tbody>\n");
                stringBuilder.append("</table>\n");
                stringBuilder.append("    </div>\n");
                stringBuilder.append("  </body>\n");
                stringBuilder.append("</html>");
                String content = stringBuilder.toString();
                MailUtil.send(email, "项目预警配信", content, true);
            });
        }
    }
}
